﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace HY.BLL
{
   public class BllAutoCode
    {

       #region 获取最大ID通过sys_code表
       public static string GetMaxBH(string table)
       {
           string rstr = ""; //前缀
           string mstr = "";//中
           string lstr = "";//后
           string cnum = "0";//当前值
           string len = "0";//总长度
           int upnum=0;
           string val = "";

           String strsql = "select * from sys_code where tabname='" + table + "'";
           DataTable dts = DAL.DbHelperSQL.SqlDataAdapter(strsql);
           if (dts.Rows.Count > 0)
           {
               rstr = dts.Rows[0]["bhleft"].ToString().Trim();
               mstr = dts.Rows[0]["bhcenter"].ToString().Trim();
               lstr = dts.Rows[0]["bhright"].ToString().Trim();
               cnum = dts.Rows[0]["bhstep"].ToString().Trim(); //当前值
               len =dts.Rows[0]["bhlenght"].ToString().Trim();

               if (cnum == "" | len == "") return "";

               val="0000000000000" + (int.Parse(cnum) + 1).ToString();
               upnum =int.Parse(val);
               val=val.Substring(val.Length-int.Parse(len),int.Parse(len));

               val = rstr + mstr + lstr + val;
               
           }

      //写入记录,保护当前编号
           if (upnum!=0)
               DAL.DbHelperSQL.ExecuteNonQuery("update sys_code set BH=N'" + val + "',  bhstep=" + upnum + " where tabname='" + table + "'");

           return val.ToString();

       }
       #endregion


    }
}
